﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GUIEXAM13
{
    public interface IViewModelMediator
    {
        void Register(IViewModel viewModel);
        void Send(string from, string to, ViewModelMessage messsage);
    }

    public class ViewModelMediator : IViewModelMediator
    {
        private readonly Dictionary<string, IViewModel> registeredViewModels = new Dictionary<string, IViewModel>();

        //Singleton
        private static volatile ViewModelMediator instance = null;
        private ViewModelMediator()
        {
        }

        public static ViewModelMediator GetInstance()
        {
            return instance ?? (instance = new ViewModelMediator());
        }

        public void Register(IViewModel viewModel)
        {
            registeredViewModels.Add(viewModel.GetName(), viewModel);
        }

        public void Send(string from, string to, ViewModelMessage messsage)
        {
            IViewModel receiver;
            if (!registeredViewModels.TryGetValue(to, out receiver))
            {
                Console.WriteLine("Receiver Not Found");
                return;
            }
            receiver.Receive(from, messsage);
        }
    }
}
